import 'dart:convert';
import 'dart:typed_data';

import 'package:pax_info_app/model/sys_info.dart';
import 'package:flutter/foundation.dart';
import 'package:pax_info_app/net/address.dart';
import 'package:pax_info_app/net/http_manager.dart';
import 'package:pax_info_app/net/resp.dart';
import 'package:encrypt/encrypt.dart' as encrypt;

class AppRepository {

  /// Returns a hex string by a `Uint8List`.
  static String formatBytesAsHexString(Uint8List bytes) {
    if (bytes == null) throw new ArgumentError("The list is null");

    var result = new StringBuffer();
    for (var i = 0; i < bytes.lengthInBytes; i++) {
      var part = bytes[i];
      result.write('${part < 16 ? '0' : ''}${part.toRadixString(16)}');
    }

    return result.toString();
  }

  static String encryptStr(String str) {
    final key = encrypt.Key.fromUtf8('123gsf78900ijk0012345yhf900mja00');
    final iv = encrypt.IV.fromUtf8('123ujh78900dfg00');

    final encrypter = encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));

    final encrypted = encrypter.encrypt(str, iv: iv);

    return formatBytesAsHexString(encrypted.bytes);

  }

  /// 登陆
  static Future<RespLogin> login({ required String username, required String password }) async {
    var res = await httpManagerNoToken.request(
        Address.kLogin,
        HttpMethod.POST,
        params: {
          "Password": encryptStr(password),
          "UUID": "",
          "UserName": encryptStr(username),
          "VerificationCode": "",
        }
    );
    return RespLogin.fromJson(jsonDecode(res.data));
  }

  /// 退出
  static Future<Resp> logout() async {
    var res = await httpManager.request(
        Address.kLogout,
        HttpMethod.POST,
    );
    return Resp.fromJson(jsonDecode(res.data));
  }

  /// 获取用户信息
  static Future<Resp> getUserInfo(int userNo, String fDate) async {
    final path = Address.addArgs(Address.kUserInfo, [userNo, fDate]);
    var res = await httpManager.request(path, HttpMethod.GET, header: {'X-Forwarded-For': '222.180.149.94'});
    return Resp.fromJson(jsonDecode(res.data));
  }

  /// 获取用户信息
  static Future<Resp> matchPerson(int userNo, { String? userName, String? imgBase64 }) async {
    Map params = {
      "UserID": userNo,
    };
    if (userName == null) {
      params['imgBase64'] = imgBase64;
    }
    if (imgBase64 == null) {
      params['UserName'] = userName;
    }
    var res = await httpManager.request(
        Address.kMatchPerson,
        HttpMethod.POST,
        params: params
    );
    return Resp.fromJson(jsonDecode(res.data));
  }

  /// 获取打卡日期段列表
  static Future<Resp> getClockDateList() async {
    final path = Address.addArgs(Address.kGetClockDateList, []);
    var res = await httpManager.request(path, HttpMethod.GET);
    return Resp.fromJson(jsonDecode(res.data));
  }

  /// 获取打卡历史信息
  static Future<Resp> getClockList(int userNo, String fDate) async {
    final path = Address.addArgs(Address.kGetClockList, [userNo, fDate]);
    var res = await httpManager.request(path, HttpMethod.GET);
    return Resp.fromJson(jsonDecode(res.data));
  }

}